//
//  HMSetAndHelpController.m
//  仿真开发
//
//  Created by LZH on 2017/3/24.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMSetAndHelpController.h"
#import "HMSetAndHelpCell.h"
#import "HMSetAndHelpController.h"
#import "HMSetAndHelpModel.h"
#import "HMFeedbackController.h"
#import "HMPasswordController.h"
#import "HMbounceTool.h"
#import "EnterMsg.h"

@interface HMSetAndHelpController ()

//数据源
@property(nonatomic,strong)NSArray  *arrList;

//要清除的的数据大小
@property(nonatomic,assign)float fileSize;


@end


static NSString *CELLID =@"CELLID";

@implementation HMSetAndHelpController

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrList=[[HMSetAndHelpModel new]getSettingModelArr];
    [self setupUI];
}


-(void)setupUI{
    //注册
    [self.tableView registerClass:[ HMSetAndHelpCell class] forCellReuseIdentifier:CELLID];
    
}



//cell的点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==0||indexPath.row==1) {
        
        HMFeedbackController*vc=[HMFeedbackController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    if (indexPath.row==2) {
        HMPasswordController*vc=[HMPasswordController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    if (indexPath.row==3) {
          [self filePath];
          [self alertClear];
        }
    
    if (indexPath.row==4) {
        
        [self alertZhuXiao];
    }
    
}




//数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrList.count ;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HMSetAndHelpCell*cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    HMSetAndHelpModel *model = _arrList[indexPath.row];
    cell.arrList=model;
    return cell ;
}






#pragma mark-清除缓存

// 显示缓存大小
-( float )filePath
{
    
    NSString * cachPath = [NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    return [ self folderSizeAtPath :cachPath];
    
}

//单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if ([manager fileExistsAtPath :filePath]){
        
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
    }
    
    return 0 ;
    
}

//遍历文件夹获得文件夹大小

- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    
    NSString * fileName;
    
    long long folderSize = 0 ;
    
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
        
    }
    
    self.fileSize = folderSize/( 1024.0 * 1024.0 );
    return folderSize/( 1024.0 * 1024.0 );
    
}


// 清理缓存

- (void)clearFile
{
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    
   // NSLog ( @"cachpath = %@" , cachPath);
    
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
            
        }
        
    }
    
    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];
    
    
    //NSLog(@"%@",[NSThread currentThread]);
}


-(void)clearCachSuccess
{
    
}


//清理缓存弹框
- (void)alertClear
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定要清理缓存？" message:[NSString stringWithFormat:@"缓存大小为%0.2fMB",self.fileSize] preferredStyle:UIAlertControllerStyleAlert];
    
    
    [alert addAction:[UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        

        [HMbounceTool CKLShowSVSuccessTitle:@"清理成功" andTime:1];
        
        //调用清理缓存
        [self clearFile];
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
}



//注销弹框
- (void)alertZhuXiao{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定注销？" message:@"退出个人账号" preferredStyle:UIAlertControllerStyleAlert];
    
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
          [HMbounceTool CKLShowSVSuccessTitle:@"注销成功" andTime:1];
        
        [self.delegate setAndHelpController:self];
        [self.navigationController popViewControllerAnimated:YES];
        EnterMsg *msg = [EnterMsg new];
        msg.enter = @"no";
        
        NSString *entPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"enter.h"];
        [NSKeyedArchiver archiveRootObject:msg toFile:entPath];
        NSLog(@"========%@",msg.enter);
 
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alert animated:YES completion:nil];
    
}


@end
